﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Graphics;

namespace AimGameLibrary.GameObject
{
    public class GameVideoPlayer : BaseGameObject
    {
        String FileVideo;
        public String PathFileVedio { get { return Script.SubVideo + "/" + Path.GetFileNameWithoutExtension(FileVideo); } }

        String PathVedio;
        public String PathVediO { get { return PathVedio; } set { PathVedio = value; } }

        Video video;
        public Video VideO { set { video = value; } }

        VideoPlayer videoplayer;
        
        //MediaState videoState;

        Texture2D videoTexture;
        public Texture2D VideoTexturE { get { return videoTexture; } }

        public GameVideoPlayer()
        {
            videoplayer = new VideoPlayer();
        }

        public override void LoadData(Identification getdata, int IndexData, NodeData node)
        {
            if (getdata.Items.GetValue(IndexData + 2).GetType() == typeof(String))
                FileVideo = (String)getdata.Items.GetValue(IndexData + 2);

            PathVedio = Path.Combine(PathVedio, Script.SubVideo);
            PathVedio = Path.Combine(PathVedio, FileVideo);

        }

        public override void UpdateTime(TimeSpan timespan)
        {
            base.UpdateTime(timespan);

            if (videoplayer.State == MediaState.Stopped)
            {
                videoplayer.IsLooped = false;
                videoplayer.Play(video);
            }

            if (videoplayer.State != MediaState.Stopped)
                videoTexture = videoplayer.GetTexture();
        }

        public override void UpdateGameTime(Microsoft.Xna.Framework.GameTime gametime)
        {
            base.UpdateGameTime(gametime);

            if (videoplayer.State == MediaState.Stopped)
            {
                videoplayer.IsLooped = false;
                videoplayer.Play(video);
            }

            if (videoplayer.State != MediaState.Stopped)
                videoTexture = videoplayer.GetTexture();
        }

        public override void UnLoadContent()
        {
            base.UnLoadContent();
            videoTexture.Dispose();
            videoplayer.Dispose();
        }
    }
}
